import java.sql.*;
import java.util.*;
import java.io.Serializable;
public class Response implements Serializable{
	// This acts as a ResultSet to the User, but it is serializable (hopefully).
    // This means transferring over web.
    
    /* methods:
     * constructor: public Response()
     *  public void addResults(ResultSet sqlset)
     * public String getString(String Key)
     * public int   getInt(String Key)
     * public float getFloat(String Key)
     * public double getDouble(String Key)
     */
    
    
    
    //The ResultSet is tightly bound with connection
	// and statement
	//This class is meant to mimic the ResultSet

    ArrayList<String> vocab;
    //Want to be able to retrieve these Strings.
	boolean isNil;
	//Is it null?
    int index;
    int maximum;
    boolean hasbeenqueried;
	ArrayList<Hashtable> results;
    Hashtable types;

    public Response(){
    }
    
	public void addResults(ResultSet rs){
        results = new ArrayList<Hashtable>();
        types = new Hashtable();
        hasbeenqueried = false;
		if (rs == null){
			isNil = true;
		}
		else{
            isNil = false;
		}
        if (!isNil){
            try{
              // Get result set meta data
              ResultSetMetaData rsmd = rs.getMetaData();
              int numColumns = rsmd.getColumnCount();
            
            
              // Get the column names; column indices start from 1
              maximum = 0;
              for (int i=1; i<numColumns + 1; i++) {
                String columnName = rsmd.getColumnName(i);
                  System.out.println("Response retrieved columnname " + columnName);
                  System.out.println("Type" +rsmd.getColumnType(i));
                types.put(columnName,new Integer(rsmd.getColumnType(i)));
              }
            
              while (rs.next()){
                  System.out.println("There is a resultset");
                maximum ++;
                Hashtable forQueue = new Hashtable();
                for (int i=1; i<numColumns +1 ; i++) {
                    System.out.println("Why wouldn't this innocous fellow run");
                    String columnName = rsmd.getColumnName(i);
                    System.out.println(columnName);
                    System.out.println((Integer)types.get(columnName));
                    int typeInt = ((Integer)types.get(columnName)).intValue();
                    System.out.println("Type name is " + typeInt);
                    if (typeInt == java.sql.Types.INTEGER){
                        forQueue.put(columnName, new Integer(rs.getInt(columnName)));
                    }
                    else if(typeInt == java.sql.Types.FLOAT){
                        forQueue.put(columnName, new Float(rs.getFloat(columnName)));
                    }
                    else if(typeInt == java.sql.Types.DOUBLE){
                        forQueue.put(columnName, new Double(rs.getDouble(columnName)));
                    }
                    else if(typeInt == java.sql.Types.VARCHAR){
                        String toput = new String(rs.getString(columnName));
                      //  forQueue.put(columnName, new String(rs.getString(columnName)));
                        forQueue.put(columnName, toput);
                        System.out.println("Adding to hashtable " + columnName + ", " +  toput  + "");
                    }
                    results.add(forQueue);
                }
              }
            }
            catch(SQLException typeexcept){
                isNil = true;
            }
        }
        index = 0;
        
	}
    public String getString(String param){
        
            if (!isNil){
                Hashtable thisrow =  results.get(index);
                String ret = thisrow.get(param).toString();
                return ret;
            }
            else {
                return null;
            }
        

    }   
    public int getInt(String param){
  
            if (!isNil){
                Hashtable thisrow =  results.get(index);
                int ret = ((Integer)thisrow.get(param)).intValue();
                return ret;
            }
            else {
                return -1;
            }
    

    }
    public boolean next(){
        if (!hasbeenqueried){
            index = 0;
        }
        else{
            index ++;
        }
        if (index < maximum){
            return true;
        }
        return false;
    }

}
